package com.util;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.extensions.RollingUpdateDaemonSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.fabric8.kubernetes.api.model.extensions.Deployment;
import io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;

public class DeploymentStatus {

    private static final Logger logger = LoggerFactory.getLogger(DeploymentStatus.class);

    public static void main(String[] args) throws InterruptedException {
        Config config = new ConfigBuilder().build();
        KubernetesClient client = new DefaultKubernetesClient(config);
            try {
                // Create a namespace for all our stuff
                Deployment deployment = new DeploymentBuilder()
                        .withNewMetadata()
                        .withName("nginx")
                        .endMetadata()
                        .withNewSpec()
                        .withReplicas(2)
                        .withNewTemplate()
                        .withNewMetadata()
                        .addToLabels("app", "nginx")
                        .endMetadata()
                        .withNewSpec()
                        .addNewContainer()
                        .withName("nginx")
                        .withImage("nginx")
                        .withCommand()
                        .addNewPort()
                        .withContainerPort(80)
                        .withProtocol("tcp")
                        .endPort()
                        .endContainer()
                        .endSpec()
                        .endTemplate()
                        .endSpec()
                        .build();
                deployment = client.extensions().deployments().inNamespace("default").create(deployment);
                log("Created deployment", deployment);

                client.services().inNamespace("default").createNew()
                        .withNewMetadata().withName("nginx").endMetadata()
                        .withNewSpec()
                        .addToSelector("app","nginx")
                        .addNewPort().withPort(80).withNewTargetPort().withIntVal(80)
                        .endTargetPort()
                        .endPort()
                        .endSpec()
                        .done();

                System.err.println("Scaling up:" + deployment.getMetadata().getName());
                //client.extensions().deployments().inNamespace("default").withName("nginx").scale(2, true);
                log("Created replica sets:", client.extensions().replicaSets().inNamespace("default").list().getItems());
                /*System.err.println("Deleting:" + deployment.getMetadata().getName());*/
                //client.resource(deployment).delete();
            } finally {
                client.close();
            }
        };

    private static void log(String action, Object obj) {
        logger.info("{}: {}", action, obj);
    }

    private static void log(String action) {
        logger.info(action);
    }
}
